package cn.ryh;

import cn.ryh.entity.User;
import cn.ryh.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) {
        String mybatisConfig = "mybatisConfig.xml";
        SqlSession sqlSession = null;
        try {
            //加载mybatis配置文件
            InputStream in = Resources.getResourceAsStream(mybatisConfig);
            //根据配置文件生成SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            //根据SqlSessionFactory获取sqlSession
            sqlSession = sqlSessionFactory.openSession();
            //通过sqlSession操作数据库
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User users = mapper.selectByPrimaryKey(1);
            System.out.println(users.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放sqlSession
            if (sqlSession != null){
                sqlSession.close();
            }
        }
    }
}
